package com.snsoft.webview;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WebviewActivity extends Activity {
	public WebView wv;
	public Dialog dialog;
	public Activity activity;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        activity = WebviewActivity.this;
        
        // Setting webivew
        wv = (WebView)findViewById(R.id.webview);
        
        wv.getSettings().setJavaScriptEnabled(true);								
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);			
        wv.getSettings().setLoadsImagesAutomatically(true);							
        wv.getSettings().setPluginsEnabled(true);									
        wv.setHorizontalScrollBarEnabled(false);									
        wv.setVerticalScrollbarOverlay(true);										
        wv.setBackgroundColor(Color.WHITE);											
        
        dialog = new Dialog(this, R.style.SNDialog);
        dialog.addContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        wv.setWebViewClient(new WebViewClient()
        {
        	// Webview handler
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url)
        	{
				if(url.startsWith("http://test"))
        		{        			                	
					Toast.makeText(activity, "Test", Toast.LENGTH_LONG).show();
        		}
				else
				{
					view.loadUrl(url);
				}
				
        		return true;
        	}
        	
        	
        	@Override     
        	public void onPageStarted(WebView view, String url, Bitmap favicon) 
        	{
        		dialog.show();
        	} 
        	
        	@Override     
        	public void onPageFinished(WebView view, String url) 
        	{
        		if(dialog.isShowing())
        			dialog.dismiss();
        	}      
        	
        	@Override
        	public void onReceivedError(WebView view, int errorCode,String description, String failingUrl)
        	{
        		wv.loadData("<html><body align='center' valign='center'></body></html>", "text/html", "UTF-8");
        		Toast.makeText(activity, "Network unavailable. Please try again later.", Toast.LENGTH_LONG).show();
        	}
        });
        
        wv.setWebChromeClient(new WebChromeClient()
        {
        	@Override
            public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
            {
                new AlertDialog.Builder(WebviewActivity.this)
                    .setTitle("Webview")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok,
                            new AlertDialog.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    result.confirm();
                                }
                            })
                    .setCancelable(false)
                    .create()
                    .show();

                return true;
            };
        });
        // loading asset
        //wv.loadUrl("file:///android_asset/index.html");
        
        // loading http
        wv.loadUrl("http://www.google.com");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	if(keyCode == KeyEvent.KEYCODE_BACK)
    	{
			new AlertDialog.Builder(this)
			.setMessage("Webview를 종료하시겠습니까?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog, int which) 
				{
					finish();
				}
			})
			.setNegativeButton("No", null).show();
			
			//wv.goBack();
    			
			return true;
    	}
    	return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
    	super.onConfigurationChanged(newConfig);
    } 
    
    @Override
	public void onDestroy() {
		super.onDestroy();
	}
}